﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace InstanceController.StateMachineDAL
{
	/// <summary>
	/// This class handles the communications between the View and the data store. 
	/// Generally this is the business layer logic.  The intent is for the View to issues 
	/// commands such as insert, update, or delete types. And to issue data queries to
	/// read from data storage.  
	/// It is expected that the developer will use the Class Diagram tool to create 
	/// any additional datamodels as necessary.
	/// 
	/// The code below is sample. Your code will/should describe your business intents.
	/// </summary>
	public static class StateDataMethods
	{
		static readonly StateDataSource StateDataSource = new StateDataSource(true);

		public static bool SaveState(StateDataModel theDataModel)
		{
			//Todo: Business Decision: 
			//Determine your PartitionKey plan and add the partition key now. Also you must add 
			//a unique rowKey.
			//You could simply hardcode it or use a web.config variable, you might even call out
			//to some more sophisticated partition key manager of your own design.  This is a 
			//business driven issue.
			//theDataModel.PartitionKey = theDataModel.State;			//Example: Grouping by State (a property in the datamodel)
			//theDataModel.RowKey = Guid.NewGuid().ToString("N");		//Example: Generating a Guid as the rowKey

			//Todo: Might want to do additional business logic here

			return StateDataSource.Create(theDataModel);
		}

		public static bool UpdateState(StateDataModel theDataModel, bool overWrite)
		{
			//Todo: Might want to do additional business logic here

			if (theDataModel == null) return false;
			if (overWrite)
			{
				if (!StateDataSource.UpdateUnconditional(theDataModel))	//if it can't be updated create a new one
					return StateDataSource.Create(theDataModel);

			}
			else
			{
				if (!StateDataSource.Update(theDataModel))				//if it can't be updated create a new one
					return StateDataSource.Create(theDataModel);
			}
			return true;
		}

		public static bool DeleteState(StateDataModel theDataModel, bool unConditional, bool asPartitionGroup)
		{
			//Todo: Might want to do additional business logic here

			if (true)	//add your evaluation here to determine when to delete (if ever)
			{
				if (asPartitionGroup)
				{
					if (StateDataSource.DeletePartitionGroupUnconditional(theDataModel) > 0)
						return true;
				}
				else
					return unConditional ? StateDataSource.DeleteUnconditional(theDataModel) : StateDataSource.Delete(theDataModel);
			}
			return true;
		}

		public static StateDataModel ReadRow(string partitionKey, string rowKey)
		{
			return StateDataSource.Read(partitionKey, rowKey);
		}

		/// <summary>
		/// Reads the StateDataModel rows using the subscriptionId and the action name such
		/// as "Increment".
		/// </summary>
		/// <param name="subscriptionID">The subscription ID.</param>
		/// <param name="actionName">Name of the action.</param>
		/// <returns></returns>
		public static List<StateDataModel> ReadRows(string subscriptionID, string actionName)
		{
			var results = StateDataSource.Read(subscriptionID + "__" + actionName).ToList();
			return results;
		}

		/// <summary>
		/// Gets the last action time tick stored in the rowKey which is the command completion time.
		/// </summary>
		/// <param name="subscriptionID">The subscription ID.</param>
		/// <param name="actionName">Name of the action.</param>
		/// <returns></returns>
		public static string GetLastActionTimeTick(string subscriptionID, string actionName)
		{
			try
			{
				var results = StateDataSource.Read(subscriptionID + "__" + actionName).ToList().OrderByDescending(e => e.RowKey).FirstOrDefault();
				return results.RowKey.Substring(0, 18);
			}
			catch (Exception)
			{
				return "0";
			}
		}

	}
}
